﻿using System;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Scalextric
{

    /// <summary>
    /// Interaction logic for RaceControl.xaml
    /// </summary>
    public partial class RaceControl : UserControl
    {
        private TimeSpan _SessionTime = new TimeSpan();
        private TimeSpan _OriginalSessionTime = new TimeSpan();
        public TimeSpan SessionTime { get { return _SessionTime; } set { _SessionTime = value; _TimeElapsed = new TimeSpan(); _OriginalSessionTime = value; } }
        private TimeSpan _TimeElapsed = new TimeSpan();
        private DispatcherTimer timer = new DispatcherTimer();
        private Track _Track = new Track();
        public Track Track { get { return _Track; } set { _Track = value; } }
        public delegate void SessionCompleteHandler(object sender, EventArgs e);
        public event SessionCompleteHandler SessionComplete;
        private bool _SessionRunning = false;
        public bool SessionRunning { get { return _SessionRunning; } set { } }

        private int _LapCount = 0;
        public int LapCount { get { return _LapCount; } set { _LapCount = value; } }

        public RaceControl()
        {
            InitializeComponent();
        }

        private string TimeString(TimeSpan time)
        {
            return String.Format("Time {0:00}:{1:00}", time.Minutes, time.Seconds);
        }

        public void SetFastestLap(TimeSpan lapTime, string Driver)
        {
            tbFLTime.Text = String.Format("{0:00}:{1:00}.{2:000}", lapTime.Minutes, lapTime.Seconds, lapTime.Milliseconds);
            tbFastestDriver.Text = Driver.Substring(0, 11);
            double speed = ((Track.Length / lapTime.TotalSeconds) * 2.2369362920544025) *32; //scaled to 1/32 //ms to mph
            tbTopSpeedMPH.Text = Math.Round(speed, 2).ToString()+" MPH";
        }

        public void SetCurrentLap(int lap)
        {
            _LapCount = lap;
            tbLapCounter.Text = String.Format("Lap {0}", _LapCount);
        }

        public void IncrementLap()
        {
            _LapCount++;
            tbLapCounter.Text = String.Format("Lap {0}", _LapCount);
        }

        private void RaiseSessionComplete()
        {
            if (SessionComplete != null)
                SessionComplete(this, EventArgs.Empty);
        }

        public void StartSession()
        {
            if(timer.IsEnabled)
                timer.Stop();
            f1RaceFlag1.SetFlag(Flags.TrackClear);
            _SessionTime = _OriginalSessionTime;
            _TimeElapsed = new TimeSpan();
            tbTimeLap.Text = TimeString(_SessionTime);
            //does time setup and begins countdown
            timer.Interval = new TimeSpan(0, 0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
            _SessionRunning = true;
        }

        public void StopSession()
        {
            timer.Stop();
            timer.Tick -= new EventHandler(timer_Tick);
            _SessionRunning = false;
            _SessionTime = _OriginalSessionTime;
            _TimeElapsed = new TimeSpan(0,0,0);
            tbTimeLap.Text = TimeString(new TimeSpan());
            tbTimeLap.Text = "Complete";
            f1RaceFlag1.SetFlag(Flags.Suspended);
            _LapCount = 0;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            //add one second
            _TimeElapsed = _TimeElapsed.Add(new TimeSpan(0, 0,0, 1));
            //remove second
            _SessionTime = _SessionTime.Subtract(new TimeSpan(0, 0, 0, 1));
            tbTimeLap.Text = TimeString(_SessionTime);
            if(_SessionTime <= new TimeSpan(0,0,0))
            {
                RaiseSessionComplete();
                timer.Stop();
                tbTimeLap.Text = "Complete";
                f1RaceFlag1.SetFlag(Flags.EndofRace);
            }
        }

        public void PauseSession()
        {
            if (_SessionRunning)
            {
                //pause session
                timer.Stop();
                _SessionRunning = false;
                tbTimeLap.Text = "Suspended";
                f1RaceFlag1.SetFlag(Flags.Danger);
            }
        }

        public void ResumeSession()
        {
            if (!_SessionRunning)
            {
                //restart session
                timer.Start();
                _SessionRunning = true;
                f1RaceFlag1.SetFlag(Flags.TrackClear);
            }
        }

    }
}
